/**
 * 
 */
package generating.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * @author bianPeng
 *
 */
public class BeanGenerateUtil {

	private Configuration cfg; 
	private String projectUrl = "H:\\xxxxxxx\\lazyDevelop"; //项目路径

	@SuppressWarnings("deprecation")
	public void init() throws IOException {
		cfg = new Configuration();
		// 设置模板存放位置
		cfg.setDirectoryForTemplateLoading(new File(projectUrl + "//src//test//java//generating//template"));
	}

	public void process(BeanGenerateUtil bgUtil) throws IOException {
		Map<String, Object> root = new HashMap<String, Object>();

		String model_name = "User";
	
		root.put("page_id", "lazy.develop");			//生成的包名
		root.put("dao_id", "lazy.develop.generator");	//dao和实体类的包名
		root.put("module_id", "id");					//主键
		root.put("model_name", "User");					//类名
		root.put("model_name_cn", "用户");				//描述
		root.put("object", "user");						//实列名
		// 项目java文件位置
		String ServicePath = projectUrl + "//src//main//java//";

		/*************** 生成Service ***************/
		String fileName = model_name + "Service.java";
		String savePath = "com//lazy//develop//service//";

		Template template = cfg.getTemplate("Service.ftl");
		bgUtil.buildTemplate(root, ServicePath, savePath, fileName, template);

		/*************** 生成ServiceImpl ***************/
		String ServiceimplPath = projectUrl + "//src//main//java//";
		fileName = model_name + "ServiceImpl.java";
		savePath = "com//lazy//develop//service//impl//";

		template = cfg.getTemplate("ServiceImpl.ftl");
		bgUtil.buildTemplate(root, ServiceimplPath, savePath, fileName, template);

		/*************** 生成Controller ***************/
		String ControllerPath = projectUrl + "//src//main//java//";
		fileName = model_name + "Controller.java";
		savePath = "com//lazy//develop//controller//";

		template = cfg.getTemplate("Controller.ftl");
		bgUtil.buildTemplate(root, ControllerPath, savePath, fileName, template);
	}

	public void buildTemplate(Map<String, Object> root, String projectPath, String savePath, String fileName, Template template) {
		String realFileName = projectPath + savePath + fileName;
		String realSavePath = projectPath + "/" + savePath;
		File newsDir = new File(realSavePath);
		if (!newsDir.exists()) {
			newsDir.mkdirs();
		}
		try {
			Writer out = new OutputStreamWriter(new FileOutputStream(realFileName), "UTF-8");
			template.process(root, out);
		} catch (Exception e) {
			System.out.println("==运行异常:"+ e);
		}
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		BeanGenerateUtil bgUtil = new BeanGenerateUtil();
		bgUtil.init();
		bgUtil.process(bgUtil);
		System.out.println("生成Bean成功");
	}
}
